package com.masterclient.app.modules.aftersale.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.aftersale.NewRefundDetailsBean;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * Created by TMVPHelper on 2017/08/27
 */
public class RefundDetailsPresenter extends RefundDetailsContract.Presenter {

    @Override
    public void getRefundDetailsInfo(String refund_no,String order_id,String order_product_id) {
        Observable<CommonBaseResponse<NewRefundDetailsBean>> observable = RetrofitUtil2.getInstance().getRefundDetailsInfo(refund_no, order_id, order_product_id);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse<NewRefundDetailsBean>>) bean -> {
            if (bean.getCode() == 0) {
                mView.showRefundDetailsInfo(bean.getData());
            } else {
                mView.showFail(bean.getMsg());
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void refundDeal(String refund_no, String operate, String operateData) {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject jsOperateData = new JSONObject(operateData);
            jsonObject.put("refund_no", refund_no);
            jsonObject.put("operate", operate);
            jsonObject.put("operate_data", jsOperateData);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseResponse> observable = RetrofitUtil2.getInstance().refundDeal(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse>) bean -> {
            if (bean.getCode() == 0) {
                mView.refundDealSuccess("");
            } else {
                mView.showFail(bean.getMsg());
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}